package com.zcl.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zcl.mapper.ClassesMapper;
import com.zcl.mapper.StudentMapper;
import com.zcl.pojo.Classes;
import com.zcl.pojo.PageResult;
import com.zcl.pojo.Student;
import com.zcl.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    //自动注入映射对象
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Override
    public List<Student> findAll() {
        //查询所有学生
        //使用自带的查询全部数据的方法(sql语句)
         List<Student> students = studentMapper.selectAll();
         //处理班级名称
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            String cname = classes.getCname();
            student.setCname(cname);
        }
        return students;
    }

    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        //分页查询,开始分页
        PageHelper.startPage(page,pageSize);
        //封装查询的条件到同一个类中
        Example example = new Example(Student.class);
        //根据所有记录查询
        List<Student> students = studentMapper.selectByExample(example);
        //处理班级名称
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            String cname = classes.getCname();
            student.setCname(cname);
        }
        //将学生结果集合转化为page集合
        Page<Student>studentPage= (Page<Student>) students;
//        返回结果
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult(),studentPage.getPages(),page);
    }

    @Override
    public PageResult<Student> search(int page, int pageSize, Student student) {
        //分页条件查询
        //分页查询,开始分页
        PageHelper.startPage(page,pageSize);
        //封装查询的条件到同一个类中
        Example example = new Example(Student.class);
        //创建封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if (student!=null){
            //判断学生姓名
            if (StringUtils.isNoneBlank(student.getSname())){
                //like模糊查询
                criteria.andLike("sname","%"+student.getSname()+"%");
            }
            if (StringUtils.isNoneBlank(student.getAddr())){
                criteria.andLike("addr","%"+student.getAddr()+"%");
            }
            if (student.getCid()!=null&&student.getCid()!=0){
                criteria.andEqualTo("cid",student.getCid());
            }
        }
        //根据所有记录查询
        List<Student> students = studentMapper.selectByExample(example);
        //处理班级名称
        for (Student st : students) {
            Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
            String cname = classes.getCname();
            st.setCname(cname);
        }
        //将学生结果集合转化为page集合
        Page<Student>studentPage= (Page<Student>) students;
//        返回结果
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult(),studentPage.getPages(),page);
    }

    @Override
    public Student findById(String sid) {
        //根据学生ID查询学生
         Student student = studentMapper.selectByPrimaryKey(sid);
         return student;
    }

    @Override
    public void insert(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    public void delete(Student student) {
        studentMapper.deleteByPrimaryKey(student);
    }
}
